package com.zhny.project.txt.model.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.txt.model.domain.TxtModel;
import com.zhny.project.txt.model.service.ITxtModelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * txt模型记录Controller
 *
 * @author yanqun
 * @date 2020-01-15
 */
@Controller
@RequestMapping("/txt/model")
public class TxtModelController extends BaseController
{
    private String prefix = "txt/model";

    @Autowired
    private ITxtModelService txtModelService;

    @RequiresPermissions("txt:model:view")
    @GetMapping()
    public String model()
    {
        return prefix + "/model";
    }

    /**
     * 查询txt模型记录列表
     */
    @RequiresPermissions("txt:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TxtModel txtModel)
    {
        startPage();
        List<TxtModel> list = txtModelService.selectTxtModelList(txtModel);
        return getDataTable(list);
    }

    /**
     * 导出txt模型记录列表
     */
    @RequiresPermissions("txt:model:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TxtModel txtModel)
    {
        List<TxtModel> list = txtModelService.selectTxtModelList(txtModel);
        ExcelUtil<TxtModel> util = new ExcelUtil<TxtModel>(TxtModel.class);
        return util.exportExcel(list, "model");
    }

    /**
     * 新增txt模型记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存txt模型记录
     */
    @RequiresPermissions("txt:model:add")
    @Log(title = "txt模型记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TxtModel txtModel)
    {
        return toAjax(txtModelService.insertTxtModel(txtModel));
    }

    /**
     * 修改txt模型记录
     */
    @GetMapping("/edit/{modelKey}")
    public String edit(@PathVariable("modelKey") String modelKey, ModelMap mmap)
    {
        TxtModel txtModel = txtModelService.selectTxtModelById(modelKey);
        mmap.put("txtModel", txtModel);
        return prefix + "/edit";
    }

    /**
     * 修改保存txt模型记录
     */
    @RequiresPermissions("txt:model:edit")
    @Log(title = "txt模型记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TxtModel txtModel)
    {
        return toAjax(txtModelService.updateTxtModel(txtModel));
    }

    /**
     * 删除txt模型记录
     */
    @RequiresPermissions("txt:model:remove")
    @Log(title = "txt模型记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(txtModelService.deleteTxtModelByIds(ids));
    }
}
